package com.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.product.enmu.DeletedEnum;
import com.product.enmu.OperatorType;
import com.product.enmu.OrderState;
import com.product.entity.OrderItem;
import com.product.mapper.OrderItemMapper;
import com.product.service.IOperationLogService;
import com.product.service.IOrderItemService;
import com.product.to.AddOrderItemTo;
import com.product.to.UpdateOrderItemTo;
import com.product.utils.PageUtils;
import com.product.vo.OrderItemVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class OrderItemServiceImpl extends ServiceImpl<OrderItemMapper, OrderItem> implements IOrderItemService {
    @Autowired
    private IOperationLogService operatorService;

    @Override
    public void deleteOrderItem(Integer orderItemId) {
        LambdaUpdateWrapper<OrderItem> updateWrapper = new LambdaUpdateWrapper<OrderItem>()
                .eq(OrderItem::getOrderItemId, orderItemId)
                .set(OrderItem::getDeleted, DeletedEnum.DELETE.getValue());
        this.baseMapper.update(updateWrapper);

        operatorService.addOperation(OperatorType.Delete.getCode(), "删除OrderItem表");
    }

    @Override
    public void deleteOrderItems(List<Integer> orderItemIds) {
        for (Integer productId : orderItemIds) {
            deleteOrderItem(productId);
        }
    }

    @Override
    public void updateOrderItem(UpdateOrderItemTo orderItem) {
        OrderItem newOrder = new OrderItem();
        BeanUtils.copyProperties(orderItem, newOrder);
        this.baseMapper.updateById(newOrder);

        operatorService.addOperation(OperatorType.Update.getCode(), "修改OrderItem表");
    }

    @Override
    public void addOrderItem(AddOrderItemTo orderItem) {
        OrderItem newOrders = new OrderItem();
        BeanUtils.copyProperties(orderItem, newOrders);
        newOrders.setState(OrderState.WAIT.getState() + "");
        this.baseMapper.insert(newOrders);

        operatorService.addOperation(OperatorType.Insert.getCode(), "添加OrderItem表");
    }

    @Override
    public PageUtils getAllOrderItem(Integer size, Integer current, Long orderId, Long productId,Long orderItemId, Integer orderState) {
        Page<OrderItem> orderItemPage = new Page<>(current, size);
        LambdaQueryWrapper<OrderItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(orderId != null, OrderItem::getOrderId, orderId);
        queryWrapper.eq(productId != null, OrderItem::getProductId, productId);
        queryWrapper.eq(orderState != null, OrderItem::getState, orderState);
        queryWrapper.eq(orderItemId != null, OrderItem::getOrderItemId, orderItemId);
        Page<OrderItem> page = this.baseMapper.selectPage(orderItemPage, queryWrapper);
        List<OrderItemVo> orderItemVoList = page.getRecords().stream().map(item -> {
            OrderItemVo orderVo = new OrderItemVo();
            BeanUtils.copyProperties(item, orderVo);
            return orderVo;
        }).toList();
        PageUtils pageUtils = new PageUtils(page);
        pageUtils.setList(orderItemVoList);
        return pageUtils;
    }

    @Override
    public List<OrderItem> getOrderById(Long orderId) {
        LambdaQueryWrapper<OrderItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OrderItem::getOrderId, orderId);
        return baseMapper.selectList(queryWrapper);
    }
}
